﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Swift;
using Swift.Lib;

namespace CB.Logic
{
    /// <summary>
    /// 游戏逻辑管理器
    /// </summary>
    public class GameManager
    {
        public static GameManager Instance = new GameManager();
        public Player Player => Player.Instance;
        public IGameData GameData => Player.GameData;

        public void StartNewGame(GameMode mode)
        {
            Player.CurGameMode = mode;
            StartNewGame();
        }

        public void StartNewGame()
        {
            CreateGame(5, 5, 6, new SRandom().Next());
            EventManager.Instance.TriggerEvent(EventTypeDefine.StartNewGame, null);
        }

        /// <summary>
        /// 创建游戏数据
        /// </summary>
        /// <param name="w">宽</param>
        /// <param name="h">长</param>
        /// <param name="c">颜色数</param>
        /// <returns></returns>
        private void CreateGame(int w, int h, int c, int srandSeed)
        {
            UnityEngine.Debug.Log($"Create game with srand seed {srandSeed}");

            switch (Player.CurGameMode)
            {
                case GameMode.Color:
                    Player.GameData = new ColorGame().Init(w, h, c, srandSeed);                    
                    break;
                case GameMode.Image:
                    Player.GameData = new ImageGame().Init(w, h, c, srandSeed);                    
                    break;
                default:
                    break;
            }
        }

        #region 移动相关
       
        /// <summary>
        /// 根据方向移动
        /// </summary>
        public bool DoMove(Direction direction)
        {
            var b = GameData.DoMove(direction, true, out Dbase db);

            if (b)
                EventManager.Instance.TriggerEvent(EventTypeDefine.BlockMove, db);

            return b;
        }

        // 尝试移动一个方块
        public bool TryMoveBlock(Block block)
        {
            var b = GameData.TryMoveBlock(block, out Dbase db);

            if (b)
                EventManager.Instance.TriggerEvent(EventTypeDefine.BlockMove, db);

            return b;
        }

        // 移动后的检查
        public void AfterMoveEndCheck()
        {
            if (GameData.GameFinish)
            {
                EventManager.Instance.TriggerEvent(EventTypeDefine.GameFinish, null);
            }
        }

        #endregion

        #region 自动寻找解法      

        

        #endregion
    }
}
